<template>
  <div>
    <el-button plain icon="el-icon-plus" @click="add">新增</el-button>

    <el-input  style="width: 50%;" placeholder="请输入食品名称" v-model="pageVo.name">
      <el-select style="width: 150px;" v-model="pageVo.typeid" slot="prepend" placeholder="所属类别">
        <el-option label="所属类别" :value="null"></el-option>
        <el-option
          v-for="(item, index) in mealstype"
          :key="index"
          :label="item.tname"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-button
        slot="append"
        type="primary"
        icon="el-icon-search"
        @click="info"
        >查询</el-button
      >
    </el-input>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="编码" prop="code" width="180">
        </el-table-column>
        <el-table-column label="食品名称" prop="name"> </el-table-column>
        <el-table-column prop="mealstype.tname" label="所属类别">
        </el-table-column>
        <el-table-column label="100g含能量" prop="energy"> </el-table-column>
        <el-table-column label="蛋白质含量" prop="protein"> </el-table-column>
        <el-table-column label="脂肪含量" prop="fat"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="success"
              plain
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              plain
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageVo.page"
        :page-sizes="[10, 20, 50]"
        :page-size="pageVo.num"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageVo.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="删除食品"
      style="border-radius: 100px"
      :visible.sync="delvisible"
      width="25%"
    >
      <span><h4>确认删除嘛？</h4></span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delvisible = false">取消</el-button>
        <el-button type="primary" @click="del">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      class="dialog"
      title="新增食品"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="meals" :rules="rules" ref="meals">
     
        <el-row>
          <el-col :span="12"> 
        <el-form-item label="食品编码" prop="code">
          <el-input class="inputForm" v-model="meals.code"></el-input>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="食品名称" prop="name">
          <el-input class="inputForm" v-model="meals.name"></el-input>
        </el-form-item>
      </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"> 
        <el-form-item label="所属类别" prop="typeid">
            <el-select class="inputForm" v-model="meals.typeid" placeholder="所属类别">
        <el-option
          v-for="(item, index) in mealstype"
          :key="index"
          :label="item.tname"
          :value="item.id"
        >
        </el-option>
      </el-select>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="每Kcal能量" prop="kcal">
          <el-input class="inputForm"
            type="number"
            v-model="meals.kcal"
          ></el-input>
        </el-form-item>
      </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"> 
        <el-form-item label="每KJ能量" prop="kj">
          <el-input
          class="inputForm"
            type="number"
            v-model="meals.kj"
          ></el-input>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="碳水化合物含量" prop="carbohydrate">
          <el-input
          class="inputForm"
            type="number"
            v-model="meals.carbohydrate"
          ></el-input>
        </el-form-item>
      </el-col>
        </el-row>


        <el-row>
          <el-col :span="12"> 
        <el-form-item label="脂肪含量" prop="fat">
          <el-input
          class="inputForm"
            type="number"
            v-model="meals.fat"
          ></el-input>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="蛋白质含量" prop="protein">
          <el-input
            type="number"
            class="inputForm"
            v-model="meals.protein"
          ></el-input>
        </el-form-item>
      </el-col>
        </el-row>

        
        <el-row>
          <el-col :span="12">
        <el-form-item label="100g含能量" prop="energy">
          <el-input
          class="inputForm"
            type="number"
            v-model="meals.energy"
          ></el-input>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="高血压" prop="hbp">
            <el-select class="inputForm" v-model="meals.hbp">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
        <el-form-item  label="糖尿病" prop="diabetes">
            <el-select class="inputForm" v-model="meals.diabetes">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="冠心病" prop="chd">
            <el-select class="inputForm" v-model="meals.chd">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
        <el-form-item label="脑卒中" prop="stroke">
            <el-select class="inputForm" v-model="meals.stroke">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>

<el-col :span="12">
        <el-form-item label="心脑血管疾病" prop="cacd">
            <el-select class="inputForm" v-model="meals.cacd">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
        <el-form-item label="高血脂" prop="hyperlipidemia">
            <el-select class="inputForm" v-model="meals.hyperlipidemia">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>

<el-col :span="8">
        <el-form-item label="肥胖" prop="obesity">
            <el-select class="inputForm" v-model="meals.obesity">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
     

      </el-col>

<el-col :span="8">
        <el-form-item label="通风" prop="ventilate">
            <el-select class="inputForm" v-model="meals.ventilate">
            <el-option label="是" :value="0"></el-option>
            <el-option label="否" :value="1"></el-option>
            </el-select>
        </el-form-item>
      </el-col>
        </el-row>

        <el-form-item label="说明" prop="explain">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 8 }"
            v-model="meals.explain"
          ></el-input>
        </el-form-item>

        <el-form-item class="left-align">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            v-show="UpMeals"
            @click="UpMealsFun('meals')"
            >确 定</el-button
          >
          <el-button
            type="primary"
            v-show="AddMeals"
            @click="AddMealsFun('meals')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      meals: { id: null,code:null,name:null,typeid:1001,energy:null,kcal:null,kj:null,
        carbohydrate:null,fat:null,protein:null,hbp:1,diabetes:1,chd:1,
        stroke:1,cacd:1,hyperlipidemia:1,obesity:1,ventilate:1,explain:null },
        rules: {
                energy: [{ required: true, message: "请输入100g含能量", trigger: "blur" }],
                code: [{ required: true, message: "请输入食品编码", trigger: "blur" }],
                name: [{ required: true, message: "请输入食品名称", trigger: "blur" }],
                kcal: [{ required: true, message: "请输入每kcal能量", trigger: "blur" }],
                kj: [{ required: true, message: "请输入每kj能量", trigger: "blur" }],
                carbohydrate: [{ required: true, message: "请输入碳水化合物含量", trigger: "blur" }],
                fat: [{ required: true, message: "请输入脂肪含量", trigger: "blur" }],
                protein: [{ required: true, message: "请输入蛋白质含量", trigger: "blur" }],
                explain: [{ required: true, message: "请输入说明", trigger: "blur" }],
            },
      tableData: [],
      mealstype: [],
      dialogFormVisible: false,
      delvisible: false,
      AddMeals:false,
      UpMeals:false,
      pageVo: { name: null, page: 1, num: 10, typeid: null, total: null },
    };
  },
  methods: {
    info() {
      const loading = this.$loading({
        lock: true,
        text: "正在显示中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.2)",
      });
      this.axios.post("/rear/LYN/mealsInfo", this.pageVo).then((req) => {
        loading.close();
        this.tableData = req.data.list;
        this.pageVo.total = req.data.total;
      });
    },
    typeMeals() {
      const loading = this.$loading({
        lock: true,
        text: "正在显示中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.2)",
      });
      this.axios.post("/rear/LYN/mealsTypeInfo", this.pageVo).then((req) => {
        loading.close();
        this.mealstype = req.data;
      });
    },
    handleDelete(index, row) {
      this.delvisible = true;
      this.meals.id = row.id;
    },
    handleSizeChange(val) {
      this.pageVo.num = val;
      this.info();
    },
    handleCurrentChange(val) {
      this.pageVo.page = val;
      this.info();
    },
    del() {
      const loading = this.$loading({
        lock: true,
        text: "正在删除中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.2)",
      });
      this.axios
        .get("/rear/LYN/delMeals", { params: { id: this.meals.id } })
        .then((req) => {
          if (req.data > 0) {
            loading.close();
            this.delvisible = false;
            this.info();
            this.$message({
              message: "删除成功",
              type: "success",
            });
          } else {
            loading.close();
          }
        })
        .catch(() => {
          loading.close();
        });
    },
    add(){
        this.dialogFormVisible=true;
        this.UpMeals=false;
        this.AddMeals=true;
        this.meals={typeid:1001,hbp:1,diabetes:1,chd:1,
        stroke:1,cacd:1,hyperlipidemia:1,obesity:1,ventilate:1}
    },
    AddMealsFun(formName){
        this.$refs[formName].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
        lock: true,
        text: "正在新增中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.2)",
      });
      this.axios.post("/rear/LYN/addMeals", this.meals).then((req) => {
        loading.close();
        if (req.data > 0) {
          this.info();
          this.dialogFormVisible = false;
          this.$message({
            message: "新增成功",
            type: "success",
          });
        }
      });
        } else {
          this.$message({
            message: "信息未输入完成",
            type: "error",
          });
          return false;
        }
      });
    },
    handleEdit(index,row){
      this.dialogFormVisible = true;
      this.UpMeals=true;
      this.AddMeals=false;
      this.meals = Object.assign({},row);
    },
    UpMealsFun(formName){
        this.$refs[formName].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
        lock: true,
        text: "正在编辑中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.2)",
      });
      this.axios.post("/rear/LYN/upMeals", this.meals).then((req) => {
        loading.close();
        if (req.data > 0) {
          this.info();
          this.dialogFormVisible = false;
          this.$message({
            message: "编辑成功",
            type: "success",
          });
        }
      });
        } else {
          this.$message({
            message: "信息未输入完成",
            type: "error",
          });
          return false;
        }
      });
    }
  },
  created() {
    this.typeMeals();
    this.info();
  },
};
</script>

<style scoped>
.left-align {
  display: flex;
  justify-content: flex-end;
}

.inputForm {
  width: 200px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.el-dialog {
  border-radius: 15px;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.el-dialog__header {
  padding: 10px 20px 10px;
  background-color: #d3dce6;
}
</style>